package logic

import (
	"2207a/bawei/api/internal/svc"
	"2207a/bawei/api/internal/types"
	"2207a/bawei/api/pkg"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"
	"path/filepath"
)

type ImageUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewImageUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageUploadLogic {
	return &ImageUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ImageUploadLogic) ImageUpload(r *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, header, err := r.FormFile("Image")
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: "上传图片错误",
			Data:    nil,
		}, nil
	}
	defer file.Close()
	ImgName := ""
	//20060102150405
	//os.WriteFile()
	FormName := filepath.FromSlash("20060102150405")

	ext := filepath.Ext(header.Filename)
	//if ext != ".mp4" && ext != ".mp3" {
	//	return &types.Response{
	//		Code:    -2,
	//		Message: "上传图片格式错误",
	//		Data:    nil,
	//	}, nil
	//}
	if header.Size > 1024*1024*3 {
		return &types.Response{
			Code:    -3,
			Message: "图片上传大小不正确",
			Data:    nil,
		}, nil
	}
	ImgName = FormName + ext

	pkg.QiNiuYUN(file, header, FormName)
	return &types.Response{
		Code:    200,
		Message: "上传成功",
		Data: map[string]interface{}{
			"image": ImgName,
		},
	}, nil
}
